<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>随机生成题目</title>
</head>
<body>
<h1>随机生成6个题目</h1>

<button onclick="generateQuestions()">生成题目</button>

<div id="questions"></div>

<div id="answerInput"></div>
<button onclick="checkAnswers()">提交答案</button>

<div id="result"></div>

<script>
const questions = [
  {question: "1 + 1 = ?", answer: "2"},
  {question: "3 * 4 = ?", answer: "12"},
  {question: "8 - 5 = ?", answer: "3"},
  {question: "10 / 2 = ?", answer: "5"},
  {question: "9 + 7 = ?", answer: "16"},
  {question: "6 * 5 = ?", answer: "30"},
  {question: "15 - 6 = ?", answer: "9"},
  {question: "20 / 4 = ?", answer: "5"},
];

let selectedQuestions = [];

function generateQuestions() {
  let shuffledQuestions = shuffleArray(questions);
  selectedQuestions = shuffledQuestions.slice(0, 6);

  let questionsContainer = document.getElementById('questions');
  questionsContainer.innerHTML = '';

  selectedQuestions.forEach((question, index) => {
    let questionElement = document.createElement('p');
    questionElement.innerText = `题目 ${index + 1}: ${question.question}`;
    questionsContainer.appendChild(questionElement);
  });

  let answerInput = document.getElementById('answerInput');
  answerInput.innerHTML = '';

  selectedQuestions.forEach((question, index) => {
    let inputElement = document.createElement('input');
    inputElement.setAttribute('type', 'text');
    inputElement.setAttribute('placeholder', '请输入答案');
    inputElement.id = `answer${index}`;
    answerInput.appendChild(inputElement);
  });
}

function checkAnswers() {
  let score = 0;
  let resultElement = document.getElementById('result');
  resultElement.innerHTML = '';

  selectedQuestions.forEach((question, index) => {
    let userAnswer = document.getElementById(`answer${index}`).value;
    if (userAnswer === question.answer) {
      score++;
    }
  });

  resultElement.innerText = `您的得分是 ${score} / 6`;
}

function shuffleArray(array) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}
</script>

</body>
</html>